<template>
  <el-dialog :title="showTitle" :visible.sync="showDialog" @close="btnCancel">
    <el-form ref="form" :model="addForm" :rules="addRules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="addForm.name" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="addForm.code" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="addForm.managerId" style="width: 80%;" size="mini" placeholder="请选择负责人">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model.trim="addForm.introduce" style="width: 80%;" size="mini" type="textarea" :rows="4" placeholder="1-100个字符" />
      </el-form-item>

    </el-form>

    <!-- <span slot="footer" class="dialog-footer">
      <el-button @click="btnCancel">取 消</el-button>
      <el-button type="primary">确 定</el-button>
    </span> -->

    <template #footer>
      <el-row v-model="addForm.managerId" style="width: 100%;" type="flex" justify="end">
        <el-button size="mini" type="text" style="color: #727479; margin-right: 20px;" @click="btnCancel">取消</el-button>
        <el-button type="primary" size="mini" @click="btnOK">确认</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addDeptApi, getDepartmentApi, getDepartmentDetailApi, getUserListApi, updateDepartmentApi } from '@/api'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      addForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      addRules: {
        name: [
          { required: true, message: '部门名字不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称2-10位字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const list = await getDepartmentApi()
              if (this.addForm.id) {
                // 编辑
                list.filter(item => item.id !== this.addForm.id).some(item => item.name === value) ? callback(new Error(`当前部门已有${value}名称`)) : callback()
              } else {
                // 新增
                list.some(item => item.name === value) ? callback(new Error(`当前部门已有${value}名称`)) : callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码2-10位字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const list = await getDepartmentApi()
              if (this.addForm.id) {
                // 编辑
                list.filter(item => item.id !== this.addForm.id).some(item => item.code === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              } else {
                // 新增
                list.some(item => item.code === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              }
            },
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍1-100位字符', trigger: 'blur' }
        ]
      },
      managerList: []
    }
  },
  // 计算属性
  computed: {
    showTitle() {
      return this.addForm.id ? '编辑部门' : '新增子部门'
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    btnCancel() {
      this.$refs.form.resetFields() // 只会清除绑定表单的数据
      this.addForm = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$emit('update:showDialog', false)
    },
    async btnOK() {
      await this.$refs.form.validate()
      let msg = ''
      if (this.addForm.id) {
        // 编辑
        msg = '编辑'
        await updateDepartmentApi(this.addForm)
      } else {
        // 新增
        msg = '新增'
        await addDeptApi({ ...this.addForm, pid: this.currentId })
      }
      this.$message.success(msg + '部门成功')
      this.btnCancel()
      // 通知父组件更新
      this.$emit('updateDept')
    },
    async getUserList() {
      this.managerList = await getUserListApi()
    },
    async getDepartmentDetail() {
      this.addForm = await getDepartmentDetailApi(this.currentId)
    }
  }
}
</script>
<style scoped>
</style>
